/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.server.view;

import java.io.IOException;
import javax.faces.event.ActionEvent;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
import offset.nodes.server.view.ContainerBean;
import offset.nodes.server.dispatch.controller.DispatchAttribute;
import offset.nodes.server.model.RepositoryUtils;
import offset.nodes.server.view.FacesUtils;
import offset.nodes.server.view.component.applet.AppletModel;

/**
 *
 * @author Walter Lütgenau
 */
public class PathBean extends ContainerBean {

    String path;
    Node node = null;
    Node previousSibling = null;
    Node nextSibling = null;
    String contentType;

    public PathBean() {
        initComponents();
    }

    protected void init() throws RepositoryException {
        DispatchAttribute att = (DispatchAttribute) FacesUtils.getRequest().getAttribute(DispatchAttribute.ATT_DISPATCH);
        if (att == null) {
            return;
        }

        // whenever current node changes, change all depending variables
        contentType = att.getContentType();
        path = att.getPath();
        if (path != null)
            node = RepositoryUtils.getRepositoryModel().getNode(path);

        if (node != null && node.getDepth() > 0) {
            Node parent = node.getParent();
            Node child = null;
            previousSibling = null;
            nextSibling = null;

            NodeIterator i = parent.getNodes();
            while (i.hasNext()) {
                child = i.nextNode();
                if (child.isSame(node))
                    break;

                previousSibling = child;
            }

            if (i.hasNext())
                nextSibling = i.nextNode();
        }
    }

    @Override
    protected void initComponents() {
        AppletModel newModel = new AppletModel();
        newModel.setRendered(false);
        getComponents().put("goto", newModel);
    }

    public void renderGoto(ActionEvent e) throws RepositoryException, IOException {
        AppletModel appletModel = (AppletModel) getComponents().get("goto");
        appletModel.setRendered(true);
        FacesUtils.refresh();
    }

    public String getPath() throws RepositoryException {
        init();
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getName() throws RepositoryException {
        init();
        if ( path == null)
            return "";
        
        if ( path.length() == 0)
            return "jcr:root";
        
        int nameStart = path.lastIndexOf("/");

        if (nameStart < 0) {
            return path;
        }

        return path.substring(nameStart + 1).replaceAll("\\+", " ");
    }
    
    public String getContentType() throws RepositoryException {
        init();
        return contentType;
    }

    public boolean isPreviousSiblingExisting() throws RepositoryException {
        init();
        return previousSibling != null;
    }

    public boolean isNextSiblingExisting() throws RepositoryException {
        init();
        return nextSibling != null;
    }

    public String getPreviousSiblingsPath() throws RepositoryException {
        if (previousSibling == null)
            return "";

        return previousSibling.getPath();
    }

    public String getNextSiblingsPath() throws RepositoryException {
        if (nextSibling == null)
            return "";

        return nextSibling.getPath();
    }
}
